﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/*
 * Author:Conis
 * Date:
 * Description:收集器，保存枚举信息
 */

namespace Conis.Squirrel.SqlBuilder
{
    /// <summary>
    /// SQL语句的条件块
    /// </summary>
    public abstract class BaseConditionBlock : ConditionUnit
    {
        /// <summary>
        /// SQL语句的条件块
        /// </summary>
        /// <param name="connector"></param>
        protected BaseConditionBlock(Connector connector)
        {
            this.Connector = connector;
        }

        /// <summary>
        /// 循环后的操作
        /// </summary>
        protected override void LoopAfter()
        {
            this.container.Append(")");
        }

        /// <summary>
        /// 循环前的操作
        /// </summary>
        protected override void LoopBefore()
        {
            this.container.Append(this.Connector.ToSql())
                .Append("(");
        }

        /// <summary>
        /// 添加一个比较单元
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connector"></param>
        /// <param name="fieldName"></param>
        /// <param name="compare"></param>
        /// <param name="value"></param>
        /// <param name="valueIsSafe"></param>
        protected abstract void AddCompareUnit<T>(Connector connector, string fieldName, CompareOperator compare, T value, bool valueIsSafe);
        /// <summary>
        /// 添加一个Between单元
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connector"></param>
        /// <param name="fieldName"></param>
        /// <param name="startValue"></param>
        /// <param name="endValue"></param>
        protected abstract void AddBetweenUnit<T>(Connector connector, string fieldName, T startValue, T endValue);
        /// <summary>
        /// 添加一个条件块
        /// </summary>
        /// <param name="connector"></param>
        /// <returns></returns>
        public abstract BaseConditionBlock AddBlock(Connector connector);

        #region BaseConditionBlock 成员

        /// <summary>
        /// 添加一个比较单元
        /// </summary>
        /// <param name="connector">连接器</param>
        /// <param name="fieldName">字段的名称</param>
        /// <param name="compare">比较器</param>
        /// <param name="value">值</param>
        public virtual void AddCompare(Connector connector, string fieldName, CompareOperator compare, int value)
        {
            this.AddCompareUnit<int>(connector, fieldName, compare, value, false);
        }

        /// <summary>
        /// 添加一个比较单元
        /// </summary>
        /// <param name="connector">连接器</param>
        /// <param name="fieldName">字段的名称</param>
        /// <param name="compare">比较器</param>
        /// <param name="value">值</param>
        public virtual void AddCompare(Connector connector, string fieldName, CompareOperator compare, string value)
        {
            this.AddCompareUnit<string>(connector, fieldName, compare, value, false);
        }

        /// <summary>
        /// 添加一个比较单元
        /// </summary>
        /// <param name="connector">连接器</param>
        /// <param name="fieldName">字段的名称</param>
        /// <param name="compare">比较器</param>
        /// <param name="value">值</param>
        public virtual void AddCompare(Connector connector, string fieldName, CompareOperator compare, bool value)
        {
            this.AddCompareUnit<bool>(connector, fieldName, compare, value, false);
        }

        /// <summary>
        /// 添加一个比较单元
        /// </summary>
        /// <param name="connector">连接器</param>
        /// <param name="fieldName">字段的名称</param>
        /// <param name="compare">比较器</param>
        /// <param name="value">值</param>
        public virtual void AddCompare(Connector connector, string fieldName, CompareOperator compare, decimal value)
        {
            this.AddCompareUnit<decimal>(connector, fieldName, compare, value, false);
        }

        /// <summary>
        /// 添加一个比较单元
        /// </summary>
        /// <param name="connector">连接器</param>
        /// <param name="fieldName">字段的名称</param>
        /// <param name="compare">比较器</param>
        /// <param name="value">值</param>
        public virtual void AddCompare(Connector connector, string fieldName, CompareOperator compare, byte value)
        {
            this.AddCompareUnit<byte>(connector, fieldName, compare, value, false);
        }

        /// <summary>
        /// 添加一个比较单元
        /// </summary>
        /// <param name="connector">连接器</param>
        /// <param name="fieldName">字段的名称</param>
        /// <param name="compare">比较器</param>
        /// <param name="value">值</param>
        public virtual void AddCompare(Connector connector, string fieldName, CompareOperator compare, long value)
        {
            this.AddCompareUnit<long>(connector, fieldName, compare, value, false);
        }

        /// <summary>
        /// 添加一个比较单元
        /// </summary>
        /// <param name="connector">连接器</param>
        /// <param name="fieldName">字段的名称</param>
        /// <param name="compare">比较器</param>
        /// <param name="value">值</param>
        public virtual void AddCompare(Connector connector, string fieldName, CompareOperator compare, DateTime value)
        {
            this.AddCompareUnit<DateTime>(connector, fieldName, compare, value, false);
        }

        /// <summary>
        /// 添加一个Between单元
        /// </summary>
        /// <param name="connector">连接器</param>
        /// <param name="fieldName">字段的名称</param>
        /// <param name="startValue">开始的值</param>
        /// <param name="endValue">结束的值</param>
        public virtual void AddBetween(Connector connector, string fieldName, DateTime startValue, DateTime endValue)
        {
           this.AddBetweenUnit<DateTime>(connector, fieldName, startValue, endValue);
        }

        /// <summary>
        /// 添加一个Between单元
        /// </summary>
        /// <param name="connector">连接器</param>
        /// <param name="fieldName">字段的名称</param>
        /// <param name="startValue">开始的值</param>
        /// <param name="endValue">结束的值</param>
        public virtual void AddBetween(Connector connector, string fieldName, int startValue, int endValue)
        {
           this.AddBetweenUnit<int>(connector, fieldName, startValue, endValue);
        }

        /// <summary>
        /// 添加一个Between单元
        /// </summary>
        /// <param name="connector">连接器</param>
        /// <param name="fieldName">字段的名称</param>
        /// <param name="startValue">开始的值</param>
        /// <param name="endValue">结束的值</param>
        public virtual void AddBetween(Connector connector, string fieldName, decimal startValue, decimal endValue)
        {
            this.AddBetweenUnit<decimal>(connector, fieldName, startValue, endValue);
        }

        /// <summary>
        /// 添加一个Between单元
        /// </summary>
        /// <param name="connector">连接器</param>
        /// <param name="fieldName">字段的名称</param>
        /// <param name="startValue">开始的值</param>
        /// <param name="endValue">结束的值</param>
        public virtual void AddBetween(Connector connector, string fieldName, long startValue, long endValue)
        {
            this.AddBetweenUnit<long>(connector, fieldName, startValue, endValue);
        }

        /// <summary>
        /// 添加一个比较单元，这个比较单元的值不作任何处理，可以实现如：field1 = field2，field1 % 2 = 0等特殊情况，但需要确保值的安全性
        /// </summary>
        /// <param name="connector">连接器</param>
        /// <param name="fieldName">字段的名称</param>
        /// <param name="startValue">开始的值</param>
        /// <param name="endValue">结束的值</param>
        public virtual void AddSafeCompare(Connector connector, string fieldName, CompareOperator compare, string value)
        {
            this.AddCompareUnit<string>(connector, fieldName, compare, value, true);
        }
        #endregion
    }
}
